#delimit ;
drop _all;

set linesize 200;

capture log close;
log using replication_output.txt, text replace;


/* MAKE FIGURE 3 (STATEWIDE OFFICES, ENTIRE TIME PERIOD) */

use replication_dataset, clear;

gen state_type = state_type2;
drop if office == "";
drop if office == "H";

foreach i in 1 2 3 4 5 6 7 8 9 10 {;
  egen dem_win_share_`i' = mean(dem_win) if abs(dem_share - .5) < `i'/100, by(state_type);
};
foreach i in 1 2 3 4 5 6 7 8 9 10 {;
  egen dem_win_share_govsen_`i' = mean(dem_win) if abs(dem_share - .5) < `i'/100 & (office == "G" | office == "S"), by(state_type);
};
foreach i in 1 2 3 4 5 6 7 8 9 10 {;
  egen dem_win_share_oth_`i'    = mean(dem_win) if abs(dem_share - .5) < `i'/100 & !(office == "G" | office == "S"), by(state_type);
};

collapse (max) dem_win_share_*, by(state_type);
reshape long dem_win_share_ dem_win_share_govsen_ dem_win_share_oth_, i(state_type) j(margin);
drop if state_type == "";

scatter dem_win_share_ margin, mlabel(state_type) mlabsize(*.75) mlabposition(0) msymbol(i) ytitle("D Share of Wins") xtitle("Window Size (%)") xlabel(1 2 3 4 5 6 7 8 9 10);
graph export figure3.pdf, replace;



/* MAIN ANALYSIS AND TABLES */

forvalues i = 1(1)4 {;

  if `i' == 1 {;
    local firstyear 1876;
    local lastyear  1945;
    local offices   SW;
  };

  if `i' == 2 {;
    local firstyear 1946;
    local lastyear  2010;
    local offices   SW;
  };

  if `i' == 3 {;
    local firstyear 1876;
    local lastyear  1945;
    local offices   H;
  };

  if `i' == 4 {;
    local firstyear 1946;
    local lastyear  2010;
    local offices   H;
  };


/* ANALYSIS */

  use replication_dataset, clear;

  drop if strpos(string(dist), "97") > 0;
  keep if type == "G";
  if "`offices'" == "H" {;
    keep if office == "H";
  };
  if "`offices'" == "SW" {;
    drop if office == "H";
  };
  keep if year >= `firstyear' & year <= `lastyear';

  gen stateyear = state + string(year);
  gen dem_share_d1 = (dem_share - .5) * (dem_share > .5);
  gen DSabs        = abs(dem_share - .5);
  gen DSabs_2      = abs(dem_share - .5)^2;
  gen DSabs_3      = abs(dem_share - .5)^3;
  gen DSabs_d1     = abs(dem_share - .5)   * (dem_share > .5);
  gen DSabs_2_d1   = abs(dem_share - .5)^2 * (dem_share > .5);
  gen DSabs_3_d1   = abs(dem_share - .5)^3 * (dem_share > .5);

  foreach j in 1 2 3 4 5 40 6 {;

    gen close = abs(dem_share - .5) < `j'/100 if dem_share != .;


/***** IMBALANCE IN LAGGED CONTROL OF GOVERNORSHIP, EVEN IN CLOSE RACES *****/

/* BASELINE RESULTS */

    if (`i' <= 2 & `j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = lagged control of gov | baseline | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  gov_dem  dem_win         if close == 1 , robust cluster(stateyear)";
               reg  gov_dem  dem_win         if close == 1 , robust cluster(stateyear);
    };

/* FIX USING POLYNOMIAL CONTROL FUNCTION */ 

    if (`i' <= 2 & (`j' == 5 | `j' == 40)) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = lagged control of gov | polynomial | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  gov_dem  dem_win  DSabs*  if close == 1 , robust cluster(stateyear)";
               reg  gov_dem  dem_win  DSabs*  if close == 1 , robust cluster(stateyear);
    };

/* FIX USING LOCAL LINEAR CONTROL FUNCTION */ 

    if (`i' <= 2 & `j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = lagged control of gov | local linear | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  gov_dem  dem_win  dem_share  dem_share_d1  if close == 1 , robust cluster(stateyear)";
               reg  gov_dem  dem_win  dem_share  dem_share_d1  if close == 1 , robust cluster(stateyear);
    };

/* FIX USING OPTIMAL BANDWIDTH */ 

    if (`i' <= 2 & `j' == 6) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = lagged control of gov | optimal bandwidth | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "rdob  gov_dem  dem_share, c(.50)";
               rdob  gov_dem  dem_share, c(.50);
    };

/* FIX USING CONTROL FOR NORMAL VOTE */

    if (`i' <= 2 & `j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = lagged control of gov | control normal vote | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  gov_dem  dem_win  st_dem  if close == 1               , robust cluster(stateyear)";
               reg  gov_dem  dem_win  st_dem  if close == 1               , robust cluster(stateyear);
      display "";
      display "**********";
      display "offices = `offices' | dep var = lagged control of gov | baseline, subsample with normal vote | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  gov_dem  dem_win          if close == 1 & st_dem != . , robust cluster(stateyear)";
               reg  gov_dem  dem_win          if close == 1 & st_dem != . , robust cluster(stateyear);

    };


/***** INCUMBENCY ADVANTAGE ESTIMATES, DEMOCRATIC VOTE SHARE IN NEXT ELECTION *****/

/* BASELINE RESULTS */

    if (`j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem vote share at t+1 | baseline | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  dem_share_next  dem_win         if close == 1 , robust cluster(stateyear)";
               reg  dem_share_next  dem_win         if close == 1 , robust cluster(stateyear);
    };

/* FIX USING POLYNOMIAL CONTROL FUNCTION */ 

    if (`j' == 5 | `j' == 40) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem vote share at t+1 | polynomial | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  dem_share_next  dem_win  DSabs*  if close == 1 , robust cluster(stateyear)";
               reg  dem_share_next  dem_win  DSabs*  if close == 1 , robust cluster(stateyear);
    };

/* FIX USING LOCAL LINEAR CONTROL FUNCTION */ 

    if (`j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem vote share at t+1 | local linear | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  dem_share_next  dem_win  dem_share  dem_share_d1  if close == 1 , robust cluster(stateyear)";
               reg  dem_share_next  dem_win  dem_share  dem_share_d1  if close == 1 , robust cluster(stateyear);
    };

/* FIX USING OPTIMAL BANDWIDTH */ 

    if (`j' == 6) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem vote share at t+1 | optimal bandwidth | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "rdob  dem_share_next  dem_share, c(.50)";
               rdob  dem_share_next  dem_share, c(.50);
    };


/***** INCUMBENCY ADVANTAGE ESTIMATES, DEMOCRATIC WIN IN NEXT ELECTION *****/

/* BASELINE RESULTS */

    if (`j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem win at t+1 | baseline | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  dem_win_next  dem_win         if close == 1 , robust cluster(stateyear)";
               reg  dem_win_next  dem_win         if close == 1 , robust cluster(stateyear);
    };

/* FIX USING POLYNOMIAL CONTROL FUNCTION */ 

    if (`j' == 5 | `j' == 40) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem win at t+1 | polynomial | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  dem_win_next  dem_win  DSabs*  if close == 1 , robust cluster(stateyear)";
               reg  dem_win_next  dem_win  DSabs*  if close == 1 , robust cluster(stateyear);
    };

/* FIX USING LOCAL LINEAR CONTROL FUNCTION */ 

    if (`j' <= 5) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem win at t+1 | local linear | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "reg  dem_win_next dem_win dem_share dem_share_d1 if close == 1 , robust cluster(stateyear)";
               reg  dem_win_next dem_win dem_share dem_share_d1 if close == 1 , robust cluster(stateyear);
    };

/* FIX USING OPTIMAL BANDWIDTH */ 

    if (`j' == 6) {;
      display "";
      display "**********";
      display "offices = `offices' | dep var = dem win at t+1 | optimal bandwidth | years = `firstyear'  - `lastyear' | threshold = `j'";
      display "rdob  dem_win_next  dem_share, c(.50)";
               rdob  dem_win_next  dem_share, c(.50);
    };

    drop close;

  };

};

log close;
